﻿namespace EasyGo.Controllers
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;
    using Resources;

    public class ResourcesController : Controller
    {
        private static string resResponse;

        public virtual ActionResult Index()
        {
            if (string.IsNullOrEmpty(resResponse))
            {
                var resourceSet =
                    Loc.ResourceManager.GetResourceSet(
                        System.Threading.Thread.CurrentThread.CurrentUICulture, true, true);

                var sb = new StringBuilder("define('loc',{");

                foreach (DictionaryEntry dictionaryEntry in resourceSet)
                {
                    if (dictionaryEntry.Value is string)
                    {
                        string value = resourceSet.GetString((string)dictionaryEntry.Key)
                                       ?? (string)dictionaryEntry.Value;

                        sb.AppendFormat(
                            "\"{0}\":\"{1}\",",
                            dictionaryEntry.Key,
                            value.Replace("\"", "\\\"").Replace('{', '[').Replace('}', ']'));
                    }
                }

                string script = sb.ToString();

                if (!string.IsNullOrEmpty(script))
                {
                    script = script.Remove(script.Length - 1);
                }

                script += "});";
                resResponse = script;
            }

            var result = new JavaScriptResult { Script = resResponse };
            return result;
        }
    }
}
